/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import me.zhengjie.annotation.Log;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.modules.system.domain.KingTeam;
import me.zhengjie.modules.system.domain.dto.KingTeamQueryCriteria;
import me.zhengjie.modules.system.service.KingTeamService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author greazy
* @date 2025-06-05
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "team")
@RequestMapping("/api/kingTeam")
public class KingTeamController {

    private final KingTeamService kingTeamService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('kingTeam:list')")
    public void exportKingTeam(HttpServletResponse response, KingTeamQueryCriteria criteria) throws IOException {
        kingTeamService.download(kingTeamService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询team")
    @PreAuthorize("@el.check('kingTeam:list')")
    public ResponseEntity<List<KingTeam>> queryKingTeam(KingTeamQueryCriteria criteria){
        List<KingTeam> kingTeams = kingTeamService.queryAll(criteria);
        return new ResponseEntity<>(kingTeams,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增team")
    @ApiOperation("新增team")
    @PreAuthorize("@el.check('kingTeam:add')")
    public ResponseEntity<Object> createKingTeam(@Validated @RequestBody KingTeam resources){
        kingTeamService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改team")
    @ApiOperation("修改team")
    @PreAuthorize("@el.check('kingTeam:edit')")
    public ResponseEntity<Object> updateKingTeam(@Validated @RequestBody KingTeam resources){
        kingTeamService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    @DeleteMapping
    public ResponseEntity<Object> deleteTeam(Integer id) {
        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(id);
        kingTeamService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}